const db = require('../db/index')
const path = require('path')

exports.addArticle = (req,res)=>{
    if(!req.file || req.file.fieldname !== 'cover_img') return res.cc('文章封面是必选参数！')
    const articleInfo = {
        ...req.body,
        //文章封面在服务器端的存放路径
        cover_img:path.join('/uploads',req.file.filename),
        //文章发布时间
        pub_date: new Date(),
        //文章作者的Id
        author_id:req.auth.id
    }
    const sql = 'insert into ev_artides set ?;'
    db.query(sql,articleInfo,(error,results)=>{
        if(error) return res.cc(error)
        if(results.affectedRows !== 1) return res.cc('发布文章失败!')
        
        res.cc('发布文章成功！',0)
    })
}